package com.lmonkey.servlet.product;

import com.lmonkey.dao.Basedao;
import com.lmonkey.servic.PRODUCTDao;
import com.lmonkey.shop.PRODUCT;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;

@WebServlet("/admin/admin_doproductdel")
public class DoProductDel extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //设置字符集
        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=utf-8");
        //连接数据库
        Connection conn = Basedao.getconn();
        //获取数据
        String b[] = request.getParameterValues("id[]");
        PreparedStatement ps = null;
        //删除图片
        for (int i = 0;i<b.length;i++){
            PRODUCT p = PRODUCTDao.selectByid(Integer.parseInt(b[i]));
            System.out.println(p+"sa");
            String path = "D:\\java\\obj\\web\\shangc\\image\\product\\"+p.getProduct_filename()+"";
            File file = new File(path);
            try {
                file.delete();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        try{
            String sql = "delete from lmonkey_product where product_id = ?";
            for (int i = 0;i<b.length;i++){
                ps = conn.prepareStatement(sql);
                ps.setInt(1,Integer.parseInt(b[i]));
                ps.executeUpdate();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        response.sendRedirect("/lyq/admin/admin_productselect");
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //设置字符集
        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=utf-8");
        //获取数据
        int id = Integer.parseInt(request.getParameter("id"));
        //链接数据库
        Connection conn = Basedao.getconn();
        PreparedStatement ps = null;
        int count = 0;
        try {
            String sql = "delete from lmonkey_product where product_id = ?";
            ps = conn.prepareStatement(sql);
            ps.setInt(1,id);
            count = ps.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }

        //获取图片路径
        String file1= request.getParameter("file");
        String path = "D:\\java\\obj\\web\\shangc\\image\\product\\"+file1+"";
        File file = new File(path);
        try {
               file.delete();
        } catch (Exception e) {
            e.printStackTrace();
        }

        PrintWriter out = response.getWriter();
        if (count>0){
           response.sendRedirect("/lyq/admin/admin_productselect?cp="+request.getParameter("cp")+"&keywords="+request.getParameter("keywords"));
        }else{
            out.println("<script>alert('用户删除失败')location.href='admin_productselect?cp="+request.getParameter("cp")+"&keywords="+request.getParameter("keywords")+"'</script>");
        }
        out.close();
    }
}
